/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 
#include "sle_hilog.h"
#include "sle_device_config.h"
#include "sle_hisysevent.h"
namespace OHOS {
namespace NearLink {
namespace SleStandard {

SleHisysevent& SleHisysevent::GetInstance()
{
    static SleHisysevent instance;
    return instance;
}

void SleHisysevent::ReportDfxEvent(const std::string &eventName, int32_t errorCode)
{
    SLE_LOGD("eventName=%{public}s, errorCode=%{public}d", eventName.c_str(), errorCode);

    std::string errorMsg;
    switch (errorCode) {
        case SLE_ERR_NOT_READY:
            errorMsg = "Service or driver not init yet";
            break;
        case SLE_ERR_ENABLE_SUCCESS_WHEN_ENABLED:
            errorMsg = "EnableSle success when sle already enabled";
            break;
        case SLE_ERR_ENABLE_FAILED_WHEN_ENABLING:
            errorMsg = "EnableSle failed when sle is enabling";
            break;
        case SLE_ERR_ENABLE_FAILED_WHEN_DISABLING:
            errorMsg = "EnableSle failed when sle is disabling";
            break;
        case SLE_ERR_DISABLE_SUCCESS_WHEN_DISABLED:
            errorMsg = "DisableSle success when sle already disabled";
            break;
        case SLE_ERR_EBABLE_SEEK_SUCCESS_WHEN_SEEK:
            errorMsg = "";
            break;
        case SLE_ERR_DISABLE_SEEK_SUCCESS_WHEN_NOT_SEEK:
            errorMsg = "";
            break;
        case SLE_ERR_EBABLE_ANNOUNCE_SUCCESS_WHEN_ANNOUNCE:
            errorMsg = "";
            break;
        case SLE_ERR_DISABLE_ANNOUNCE_SUCCESS_WHEN_NOT_ANNOUNCE:
            errorMsg = "";
            break;
        default:
            return;
    }

    SLE_LOGD("errorMsg=%{public}s", errorMsg.c_str());

    int32_t ret = HiSysEventWrite(
            SLE_DOMAIN,
            eventName,
            OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
            "ERROR_CODE", errorCode,
            "ERROR_MSG", errorMsg);

    if (ret != SLE_NO_ERROR) {
        SLE_LOGE("HiSysEventWrite failed, res:%{public}d", ret);
    }
}
} // namespace SleStandard
} // namespace NearLink
} // namespace OHOS